//package com.lonely.framework.web.service;
//
//import cn.hutool.extra.spring.SpringUtil;
//import com.lonely.common.constant.Constants;
//import com.lonely.common.core.domain.auth.LoginBodyDto;
//import com.lonely.common.core.domain.auth.LoginUser;
//import com.lonely.common.core.domain.auth.UserMsgDto;
//import com.lonely.system.domain.entity.SysUser;
//import com.lonely.common.exception.AuthException;
//import com.lonely.common.utils.DateUtils;
//import com.lonely.common.utils.MessageUtils;
//import com.lonely.common.utils.ServletUtils;
//import com.lonely.common.utils.ip.IpUtils;
//import com.lonely.framework.manager.AsyncManager;
//import com.lonely.framework.manager.factory.AsyncFactory;
//import com.lonely.system.service.ISysUserService;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.exception.ExceptionUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
///**
// * 登录校验方法
// *
// * @author fzzf
// */
//@Component
//@Slf4j
//public class SysLoginService {
//
//    @Autowired
//    private TokenService tokenService;
//
//    @Autowired
//    private ISysUserService userService;
//
//    @Autowired
//    private UserSaService userSaService;
//
//    /**
//     * 登录验证
//     *
//     * @param loginBody 请求实体
//     * @return 结果
//     */
//    public String login(LoginBodyDto loginBody) {
//        LoginUser loginUser;
//        try {
//            loginUser = userSaService.loadUserName(loginBody.getPhone(), loginBody.getPassword());
//        } catch (Exception e) {
//            log.error("登录失败,异常原因：{}", ExceptionUtils.getStackTrace(e));
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginBody.getPhone(), Constants.LOGIN_FAIL, e.getMessage()));
//            throw new AuthException(e.getMessage());
//        }
//        AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginBody.getPhone(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
//        recordLoginInfo(loginUser.getUser());
//        // 生成token
//        return tokenService.createToken(loginUser);
//    }
//
//    /**
//     * 记录登录信息
//     */
//    public void recordLoginInfo(UserMsgDto user) {
//        user.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
//        user.setLoginDate(DateUtils.getNowDate());
//
//        SysUser oldUser = this.userService.getById(user.getUserId());
//        oldUser.setLoginIp(user.getLoginIp());
//        oldUser.setLoginDate(user.getLoginDate());
//        userService.updateById(oldUser);
//    }
//}
